package com.gzxx.util.excelutil;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Pengpdx
 * @description: EasyExcel读取工具类
 * @date 2020/9/21 16:58
 */
public class EasyExcelReadUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(EasyExcelReadUtil.class);


    /**
     * 默认读取第一页Sheet
     * @param file              文件
     * @param clazz               实体类
     * @return
     */
    public static List<Object> readExcel(MultipartFile file, Class<?> clazz) {
        ExcelListener excelListener = new ExcelListener();
        ExcelReader excelReader = null;
        try {
            excelReader = getReader(file, clazz, excelListener);
            ReadSheet readSheet = EasyExcel.readSheet(0).build();
            excelReader.read(readSheet);
        } finally {
            if (excelReader != null) {
                // 这里千万别忘记关闭，读的时候会创建临时文件，到时磁盘会崩的
                excelReader.finish();
            }
        }
        if (excelListener == null){
            return null;
        }
        return excelListener.getDatas();
    }

    /**
     * 导出指定 Sheet
     * @param file              文件
     * @param clazz               实体类
     * @param sheetNo           sheet 的序号 从0开始
     * @return
     */
    public static List<Object> readExcel(MultipartFile file, Class<?> clazz, int sheetNo) {
        ExcelListener excelListener = new ExcelListener();
        ExcelReader excelReader = null;
        try {
            excelReader = getReader(file, clazz, excelListener);
            ReadSheet readSheet = EasyExcel.readSheet(sheetNo).build();
            excelReader.read(readSheet);
        } finally {
            if (excelReader != null) {
                // 这里千万别忘记关闭，读的时候会创建临时文件，到时磁盘会崩的
                excelReader.finish();
            }
        }
        if (excelListener == null){
            return null;
        }
        return excelListener.getDatas();
    }

    /**
     * 导出指定 Sheet
     * @param file              文件
     * @param clazz               实体类
     * @param sheetNos           sheet 的序号 从0开始
     * @return
     */
    public static List<Object> readExcel(MultipartFile file, Class<?> clazz, int ...sheetNos) {
        ExcelListener excelListener = new ExcelListener();
        ExcelReader excelReader = null;
        try {
            excelReader = EasyExcel.read(file.getOriginalFilename()).build();
            List<ReadSheet> sheetList = new ArrayList<>(sheetNos.length);
            for(int sheetNo : sheetNos){
                ReadSheet readSheet =
                        EasyExcel.readSheet(sheetNo).head(clazz).registerReadListener(new ExcelListener()).build();
                sheetList.add(readSheet);
            }
            excelReader.read(sheetList);
        } finally {
            if (excelReader != null) {
                // 这里千万别忘记关闭，读的时候会创建临时文件，到时磁盘会崩的
                excelReader.finish();
            }
        }
        if (excelListener == null){
            return null;
        }
        return excelListener.getDatas();
    }


    /**
     * 返回 ExcelReader
     *
     * @param excel         需要解析的 Excel 文件
     * @param excelListener new ExcelListener()
     */
    /**
     * 返回 ExcelReader
     * @param excel 需要解析的 Excel 文件
     * @param clazz   实体类
     * @param excelListener new ExcelListener()
     * @return
     */
    private static ExcelReader getReader(MultipartFile excel, Class<?> clazz,
                                         ExcelListener excelListener) {
        String fileName = excel.getOriginalFilename();
        //校验文件类型
        if (fileName == null || (!fileName.toLowerCase().endsWith(ExcelTypeEnum.XLSX.getValue())
                && !fileName.toLowerCase().endsWith(ExcelTypeEnum.XLS.getValue()))) {
            throw new ExcelException("文件格式错误！");
        }
        ExcelReader excelReader = EasyExcel.read(fileName, clazz, excelListener).build();
        return excelReader;
    }

}
